//
//  MyLevelViewController.m
//  iOS_beboy
//
//  Created by admin on 2017/11/14.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyLevelViewController.h"
#import "BDUserModel.h"

@interface BDMyLevelViewController ()
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation BDMyLevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = ASLocalizedString(@"等级");
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.titleArray = [NSMutableArray arrayWithObjects:ASLocalizedString(@"驾驶学员"),ASLocalizedString(@"新手上路"),ASLocalizedString(@"模范驾驶员"),ASLocalizedString(@"狂野老司机"),ASLocalizedString(@"秋名山车神"), nil];
    
    self.scrollView = [[UIScrollView alloc] init];
    _scrollView.left = 0;
    _scrollView.top = kBDTopHeight;
    _scrollView.size = CGSizeMake(kBDScreenWidth, kBDScreenHeight - kBDTopHeight);
    [self.view addSubview:_scrollView];
    
    [self createUI];
}

-(void)createUI{
    
    BDUserModel *user = [BDUserModel getCurrentUser];
    self.dict = user.points.mutableCopy;
    
    UIView *topView = [[UIView alloc] init];
    topView.left = 0;
    topView.top = 0;
    topView.size = CGSizeMake(kBDScreenWidth, 130);
    topView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:topView];
    
    UIImageView *level1ImageView = [[UIImageView alloc] init];
    level1ImageView.top = 10;
    level1ImageView.left = 30;
    level1ImageView.size = CGSizeMake(43, 28);
    level1ImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"level%@",self.dict[@"level"]]];
    [topView addSubview:level1ImageView];
    
    UIImageView *nextLevelImageView = [[UIImageView alloc] init];
    nextLevelImageView.top = 10;
    nextLevelImageView.size = CGSizeMake(43, 28);
    nextLevelImageView.right = kBDScreenWidth - 30;
    if ([self.dict[@"level"] integerValue] == 5) {
        nextLevelImageView.image = [UIImage imageNamed:@"5.5"];
    }else{
        nextLevelImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.%ld",[self.dict[@"level"] integerValue] + 1,[self.dict[@"level"] integerValue] + 1]];
    }
    [topView addSubview:nextLevelImageView];
    
    UIImageView *arrowsImageView = [[UIImageView alloc] init];
    arrowsImageView.left = level1ImageView.right + 20;
    arrowsImageView.top = level1ImageView.bottom - 5;
    arrowsImageView.image = [UIImage imageNamed:@"222"];
    arrowsImageView.size = CGSizeMake(kBDScreenWidth - arrowsImageView.left * 2, 10);
    [topView addSubview:arrowsImageView];
    
    UILabel *allEXP = [[UILabel alloc] init];
    allEXP.text = [NSString stringWithFormat:ASLocalizedString(@"目前%@(EXP)"),self.dict[@"total_point"]];
    allEXP.textColor = [UIColor colorWithHexString:@"f6c756"];
    allEXP.font = [UIFont systemFontOfSize:10];
    allEXP.textAlignment = NSTextAlignmentCenter;
    allEXP.size = CGSizeMake(arrowsImageView.width, 10);
    allEXP.left = arrowsImageView.left;
    allEXP.bottom = arrowsImageView.top - 4;
    [topView addSubview:allEXP];
    
    UILabel *shortLabel = [[UILabel alloc] init];
    shortLabel.text = [NSString stringWithFormat:ASLocalizedString(@"还差%@EXP就升级了"),self.dict[@"point_to_next"]];
    shortLabel.textColor = [UIColor colorWithHexString:@"cccccc"];
    shortLabel.font = [UIFont systemFontOfSize:8];
    shortLabel.textAlignment = NSTextAlignmentCenter;
    shortLabel.size = CGSizeMake(arrowsImageView.width, 10);
    shortLabel.left = arrowsImageView.left;
    shortLabel.top = arrowsImageView.bottom + 4;
    [topView addSubview:shortLabel];
    
    UILabel *firstLabel = [[UILabel alloc] init];
    firstLabel.text = self.titleArray[[self.dict[@"level"] intValue] - 1];
    firstLabel.textColor = [UIColor colorWithHexString:@"333333"];
    firstLabel.font = [UIFont systemFontOfSize:13];
    [firstLabel sizeToFit];
    firstLabel.top = level1ImageView.bottom + 10;
    firstLabel.centerX = level1ImageView.centerX;
    [topView addSubview:firstLabel];
    
    UILabel *secondLabel = [[UILabel alloc] init];
    if ([self.dict[@"level"] intValue] != 5) {
        secondLabel.text = self.titleArray[[self.dict[@"level"] intValue]];
    }else{
        secondLabel.text = self.titleArray[[self.dict[@"level"] intValue] - 1];
    }
    secondLabel.textColor = [UIColor colorWithHexString:@"33333333"];
    secondLabel.font = [UIFont systemFontOfSize:13];
    [secondLabel sizeToFit];
    secondLabel.top = level1ImageView.bottom + 10;
    secondLabel.centerX = nextLevelImageView.centerX;
    [topView addSubview:secondLabel];
    
    CGFloat point = [self.dict[@"total_point"] floatValue];

    CGFloat finallyWidth = 0.0;
    if (point <= 100) {
        finallyWidth = point / 100.0 * (27 + (kBDScreenWidth - 27 * 2) / 4 - 27);
    }else if (point > 100 && point <= 1000){
        finallyWidth = ((point - 100) / 900 + 1) * (27 + (kBDScreenWidth - 27 * 2) / 4 - 27);
    }else if (point > 1000 && point<= 5000){
        finallyWidth = ((point - 1000) / 4000 + 2) * (27 + (kBDScreenWidth - 27 * 2) / 4 - 27);
    }else if (point > 5000 && point <= 30000){
        finallyWidth = ((point - 5000) / 25000 + 3) * (27 + (kBDScreenWidth - 27 * 2) / 4 - 27);
    }else{
        finallyWidth = kBDScreenWidth - 28 * 2;
    }
    
    UIView *leftView = [[UIView alloc] init];
    leftView.left = 28;
    leftView.top = firstLabel.bottom + 30;
    leftView.width = finallyWidth;
    leftView.height = 8;
    
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.fillColor = [UIColor colorWithHexString:@"89b5fc"].CGColor;
    layer.strokeColor = [UIColor colorWithHexString:@"89b5fc"].CGColor;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(leftView.width, 4) radius:5 startAngle:-M_PI * 0.75 endAngle:M_PI * 0.75 clockwise:YES];
    layer.path = path.CGPath;
    
    CAGradientLayer *gradLayer = [CAGradientLayer layer];
    gradLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"d3b0fe"].CGColor,(__bridge id)[UIColor colorWithHexString:@"89b5fc"].CGColor];
    gradLayer.startPoint = CGPointMake(0, 0);
    gradLayer.endPoint = CGPointMake(1.0, 0);
    gradLayer.frame = leftView.bounds;
    
    UIView *slideView = [[UIView alloc] init];
    slideView.left = leftView.right;
    slideView.top = firstLabel.bottom + 30;
    slideView.size = CGSizeMake(kBDScreenWidth - 28 * 2 - leftView.width, 8);
    slideView.backgroundColor = [UIColor colorWithHexString:@"d7e9ff"];
    [topView addSubview:slideView];
    
    [leftView.layer addSublayer:layer];
    [leftView.layer addSublayer:gradLayer];
    [topView addSubview:leftView];
    
    
    for (int i = 1; i <= 5; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        UILabel *levelLabel = [[UILabel alloc] init];
        levelLabel.font = [UIFont systemFontOfSize:10];
        levelLabel.text = self.titleArray[(i-1)];
        [levelLabel sizeToFit];
        if (i == [self.dict[@"level"] intValue]) {
            imageView.size = CGSizeMake(76 / 2, 50 / 2);
            imageView.bottom = slideView.bottom + 4;
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.%d.%d",i,i,i]];
            levelLabel.textColor = [UIColor colorWithHexString:@"4b75a4"];
        }else{
            imageView.size = CGSizeMake(70 / 2, 40 / 2);
            imageView.bottom = slideView.bottom + 3;
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.%d.%d.%d",i,i,i,i]];
            levelLabel.textColor = [UIColor colorWithHexString:@"666666"];
        }
        imageView.centerX = 27 + (i - 1) * (kBDScreenWidth - 27 * 2) / 4;
        levelLabel.top = imageView.bottom + 5;
        levelLabel.centerX = imageView.centerX;
        [topView addSubview:imageView];
        [topView addSubview:levelLabel];
    }
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.left = 0;
    bottomView.top = topView.bottom + 10;
    bottomView.width = kBDScreenWidth;
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:bottomView];
    
    UILabel *ruleLabel = [[UILabel alloc] init];
    ruleLabel.left = 10;
    ruleLabel.top = 10;
    ruleLabel.text = ASLocalizedString(@"规则说明!");
    ruleLabel.textColor = [UIColor colorWithHexString:@"f6c756"];
    ruleLabel.font = [UIFont systemFontOfSize:14];
    [ruleLabel sizeToFit];
    [bottomView addSubview:ruleLabel];
    
    UIImageView *ruleImageView = [[UIImageView alloc] init];
    ruleImageView.left = 10;
    ruleImageView.top = ruleLabel.bottom + 10;
    ruleImageView.size = CGSizeMake(kBDScreenWidth - 20, 322 / 2);
    ruleImageView.image = [UIImage imageNamed:@"shuoming"];
    [bottomView addSubview:ruleImageView];
    
    UILabel *acquireLabel = [[UILabel alloc] init];
    acquireLabel.text = ASLocalizedString(@"EXP获取路径:");
    acquireLabel.textColor = [UIColor colorWithHexString:@"f6c756"];
    acquireLabel.font = [UIFont systemFontOfSize:15];
    [acquireLabel sizeToFit];
    acquireLabel.left = 10;
    acquireLabel.top = ruleImageView.bottom + 10;
    [bottomView addSubview:acquireLabel];
    
    
  
    NSString *string = ASLocalizedString(@"1.用户在论坛版块发帖、回复，能获得EXP。");
    NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange ranger = [string rangeOfString:ASLocalizedString(@"发帖、回复")];
    [mutableString addAttributes:@{NSForegroundColorAttributeName:KBDColorBlue} range:ranger];
    
    UILabel *explainLabel = [[UILabel alloc] init];
    explainLabel.font = [UIFont systemFontOfSize:12];
    explainLabel.textColor = [UIColor colorWithHexString:@"999999"];
    explainLabel.attributedText = mutableString;
    [explainLabel sizeToFit];
    explainLabel.left = 10;
    explainLabel.top = acquireLabel.bottom + 15;
    [bottomView addSubview:explainLabel];
    
    NSString *subtitleString = ASLocalizedString(@"1）用户在论坛版块进行发帖、回复他人的帖子，能获得一定量EXP。\n2）用户发帖被评为精品贴，有额外EXP奖励。\n3）用户通过论坛版块所获得EXP，延迟最多24小时到账。\n4）用户每天通过论坛版块最多能获得40点EXP。\n5）用户所发的贴如果被定义为水帖，或内容虚假，该贴已获得EXP将扣除。");
    NSMutableParagraphStyle *partStrle = [[NSMutableParagraphStyle alloc] init];
    partStrle.firstLineHeadIndent = 0;
    partStrle.headIndent = 20;
    NSMutableAttributedString *subtitleAttString = [[NSMutableAttributedString alloc] initWithString:subtitleString attributes:@{NSParagraphStyleAttributeName:partStrle}];
    
    UILabel *subtitleLabel = [[UILabel alloc] init];
    subtitleLabel.left = 20;
    subtitleLabel.top = explainLabel.bottom;
    subtitleLabel.font = [UIFont systemFontOfSize:12];
    subtitleLabel.textColor = [UIColor colorWithHexString:@"999999"];
    subtitleLabel.numberOfLines = 0;
    subtitleLabel.width = kBDScreenWidth - 30;
    subtitleLabel.attributedText = subtitleAttString;
    subtitleLabel.height = [subtitleLabel.text heightForFont:[UIFont systemFontOfSize:12] width:subtitleLabel.width];
    [bottomView addSubview:subtitleLabel];
    
    NSString *twoString = ASLocalizedString(@"2.用户购买套餐在商家消费，验证成功后立刻获得EXP。1元RMB对应获得1点EXP。");
    NSMutableParagraphStyle *paratStrle = [[NSMutableParagraphStyle alloc] init];
    paratStrle.firstLineHeadIndent = 0;
    paratStrle.headIndent = 10;
    NSRange rang = [twoString rangeOfString:ASLocalizedString(@"商家消费")];
    NSMutableAttributedString *twoAttString = [[NSMutableAttributedString alloc] initWithString:twoString];
    [twoAttString addAttributes:@{NSForegroundColorAttributeName:KBDColorBlue} range:rang];
    [twoAttString addAttributes:@{NSParagraphStyleAttributeName:paratStrle} range:NSMakeRange(0, twoAttString.length)];
    
    UILabel *twoLabel = [[UILabel alloc] init];
    twoLabel.left = 10;
    twoLabel.top = subtitleLabel.bottom;
    twoLabel.font = [UIFont systemFontOfSize:12];
    twoLabel.textColor = [UIColor colorWithHexString:@"999999"];
    twoLabel.attributedText = twoAttString;
    twoLabel.numberOfLines = 0;
    twoLabel.width = kBDScreenWidth - 20;
    twoLabel.height = [twoLabel.text heightForFont:[UIFont systemFontOfSize:12] width:twoLabel.width];
    [bottomView addSubview:twoLabel];
    
    UILabel *achievementLabel = [[UILabel alloc] init];
    achievementLabel.font = [UIFont systemFontOfSize:15];
    achievementLabel.textColor = [UIColor colorWithHexString:@"f6c756"];
    achievementLabel.text = ASLocalizedString(@"成就奖励:");
    [achievementLabel sizeToFit];
    achievementLabel.left = 10;
    achievementLabel.top = twoLabel.bottom + 15;
    [bottomView addSubview:achievementLabel];
    
    NSString *detailstring = ASLocalizedString(@"当达到”新手上路“及以上成就后，根据不同称号等级，可以每周领取一定金额的店铺通用红包，可在商家消费中抵扣。");
    NSMutableParagraphStyle *detailsStyle = [[NSMutableParagraphStyle alloc] init];
    detailsStyle.firstLineHeadIndent = 10;
    NSRange detailsRang = [detailstring rangeOfString:ASLocalizedString(@"不同称号等级")];
    NSRange secondDetailsRang = [detailstring rangeOfString:ASLocalizedString(@"通用红包")];
    NSMutableAttributedString *detailsAttString = [[NSMutableAttributedString alloc] initWithString:detailstring];
    [detailsAttString addAttributes:@{NSForegroundColorAttributeName:KBDColorBlue} range:detailsRang];
    [detailsAttString addAttributes:@{NSForegroundColorAttributeName:KBDColorBlue} range:secondDetailsRang];
    [detailsAttString addAttributes:@{NSParagraphStyleAttributeName:detailsStyle} range:NSMakeRange(0, detailstring.length)];
    
    UILabel *detailsLabel = [[UILabel alloc] init];
    detailsLabel.left = 10;
    detailsLabel.top = achievementLabel.bottom + 15;
    detailsLabel.font = [UIFont systemFontOfSize:12];
    detailsLabel.textColor = [UIColor colorWithHexString:@"999999"];
    detailsLabel.numberOfLines = 0;
    detailsLabel.attributedText = detailsAttString;
    detailsLabel.width = kBDScreenWidth - 20;
    detailsLabel.height = [detailsLabel.text heightForFont:[UIFont systemFontOfSize:12] width:detailsLabel.width];
    [bottomView addSubview:detailsLabel];
    
    bottomView.height = detailsLabel.bottom + 15;
    self.scrollView.contentSize = CGSizeMake(0, bottomView.height + bottomView.top);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
